﻿using Common;
using Entity;
using Microsoft.AspNetCore.Mvc;
using Renovation.Models;

namespace Renovation.Controllers
{
    /// <summary>
    /// 材料
    /// </summary>
    public class MaterialController : Controller
    {
        /// <summary>
        /// 材料列表页面
        /// </summary>
        /// <returns></returns>
        public ViewResult MaterialListView()
        {
            return View();
        }
        /// <summary>
        /// 材料添加页面
        /// </summary>
        /// <returns></returns>
        public ViewResult CreateMaterialView()
        {
            return View();
        }
        /// <summary>
        /// 修改材料页面
        /// </summary>
        /// <returns></returns>
        public ViewResult UpdateMaterialView()
        {
            return View();
        }
        /// <summary>
        /// 添加材料
        /// </summary>
        /// <returns></returns>
        public APIResultModel CreateMaterial(string name, string unit, decimal price, decimal priceMain, decimal priceArtificial, string remark)
        {
            //参数验证
            if (string.IsNullOrEmpty(name))
            {
                return APIResultModel.GetErrorResult("请输入材料名称");
            }
            if (price < 0 || priceMain < 0 || priceArtificial < 0)
            {
                return APIResultModel.GetErrorResult("价格和成本的金额必须大于零");
            }
            if (price != priceMain+priceArtificial)
            {
                return APIResultModel.GetErrorResult("价格必须等于主要成本和人工成本");
            }
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            //创建材料
            Material Material = new Material();
            Material.Id = Guid.NewGuid().ToString();
            Material.Name = name;
            Material.Unit = unit;
            Material.Price = price;
            Material.PriceMain = priceMain;
            Material.PriceArtificial = priceArtificial;
            Material.Remark = remark;
            Material.CreateTime = DateTime.Now;

            //向数据集添加
            dbContext.Materials.Add(Material);
            //保存
            dbContext.SaveChanges();

            return APIResultModel.GetSuccessResult("注册成功");
        }
        /// <summary>
        /// 获取一个材料的数据
        /// </summary>
        /// <returns></returns>
        public APIResultModel GetMaterial(string id)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            //查看一条数据
            Material Material = dbContext.Materials.FirstOrDefault(e => e.Id == id);
            return APIResultModel.GetSuccessResult("成功", Material);
        }
        /// <summary>
        /// 获取多条数据
        /// </summary>
        /// <returns></returns>
        public object GetMaterialList(int page, int limit, string name)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            //还没有查询出来的数据集
            IQueryable<Material> list = dbContext.Materials.Where(e => e.IsDelete == false);
            //name有数据
            if (!string.IsNullOrEmpty(name))
            {

                list = list.Where(e => e.Name.Contains(name));
            }

            //先排序再分页，OrderBy升序，OrderByDescending降序
            List<MaterialModel> res = list.OrderByDescending(e => e.CreateTime).Skip((page - 1) * limit).Take(limit).Select(e => new MaterialModel()
            {
                Id = e.Id,
                Name = e.Name,
                Unit = e.Unit,
                Price = e.Price,
                PriceMain = e.PriceMain,
                PriceArtificial = e.PriceArtificial,
                Remark = e.Remark,
                CreateTime = e.CreateTime.ToString("yyyy-MM-dd hh:mm:ss"),
            }).ToList();
            //获取一共有多少条数据
            int count = list.Count();
            return APIResultModel.GetSuccessResultForLayui("成功", count, res);

        }

        /// <summary>
        /// 删除一个材料
        /// </summary>
        /// <returns></returns>
        public APIResultModel DeleteMaterial(string id)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            //先查询材料
            Material Material = dbContext.Materials.FirstOrDefault(e => e.Id == id);

            if (Material != null)
            {
                //材料存在删除
                Material.IsDelete = true;
                Material.DeleteTime = DateTime.Now;
                //保存
                dbContext.SaveChanges();

                return APIResultModel.GetSuccessResult("成功");
            }
            else
            {
                //材料不存在报错
                return APIResultModel.GetErrorResult("失败");
            }


        }
        /// <summary>
        /// 批量删除材料
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public APIResultModel BatchDeleteMaterial(List<string> ids)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            foreach (var id in ids)
            {
                //先查询材料是否存在
                Material Material = dbContext.Materials.FirstOrDefault(e => e.Id == id);

                if (Material != null)
                {
                    Material.IsDelete = true;
                    Material.DeleteTime = DateTime.Now;
                }
            }
            //保存
            int index = dbContext.SaveChanges();
            return APIResultModel.GetSuccessResult("成功");
        }
        /// <summary>
        /// 修改材料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public APIResultModel UpdateMaterial(string id,string name, string unit, decimal price, decimal priceMain, decimal priceArtificial, string remark)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();
            if (string.IsNullOrEmpty(name))
            {
                return APIResultModel.GetErrorResult("请输入材料名称");
            }
            //先查询材料
            Material Material = dbContext.Materials.FirstOrDefault(e => e.Id == id);

            if (Material != null)
            {
                //材料存在修改
                Material.Name = name;
                Material.Unit = unit;
                Material.Price = price;
                Material.PriceMain = priceMain;
                Material.PriceArtificial = priceArtificial;
                Material.Remark = remark;
                int index = dbContext.SaveChanges();
                return APIResultModel.GetSuccessResult("成功");
            }
            else
            {
                //材料不存在报错
                return APIResultModel.GetSuccessResult("失败");
            }

        }
        /// <summary>
        /// 获取材料下拉选择的集合
        /// </summary>
        /// <returns></returns>
        public APIResultModel GetSelectOptions()
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();
            var res = dbContext.Materials.Where(e => e.IsDelete == false).Select(r => new
            {
                Title = r.Name,
                Value = r.Id
            }).ToList();
            return APIResultModel.GetSuccessResult("成功", res);
        }
    }
}
